//
//  CommonPickerView.m
//  DailyRecord
//
//  Created by Gin on 2022/3/31.
//

#import "CommonPickerView.h"

@interface CommonPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIPickerView *pickerView;

@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) UIButton *finishBtn;

@property (nonatomic, strong) NSMutableDictionary *contentDic;

@end

@implementation CommonPickerView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = kColorWithHex(0x000000, 0.3);
    }
    return self;
}

- (void)initUI{
    [self addSubview:self.bgView];
    [self addSubview:self.pickerView];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.finishBtn];
}

- (void)setUpContraints{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.mas_equalTo(200);
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.bgView);
        make.size.mas_equalTo(CGSizeMake(50, kSmallButtonHeight));
    }];
    
    [self.finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.bgView);
        make.size.equalTo(self.cancelBtn);
    }];
    
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.equalTo(self.cancelBtn.mas_bottom);
    }];
}

#pragma mark -- UIPickerViewDelegate,UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return self.dataSource.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataSource[component].count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return self.dataSource[component][row].description;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    [self.contentDic setObject:self.dataSource[component][row] forKey:@(component)];
}

#pragma mark -- setter&&getter

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton new];
        [_cancelBtn setTitleColor:kColorWithHex(0x1296db, 1) forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        @weakify(self);
        [[_cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self removeFromSuperview];
        }];
    }
    return _cancelBtn;
}

#pragma mark -- setter&&getter

- (UIButton *)finishBtn{
    if (!_finishBtn) {
        _finishBtn = [UIButton new];
        [_finishBtn setTitleColor: kColorWithHex(0x1296db, 1)  forState:UIControlStateNormal];
        [_finishBtn setTitle:@"完成" forState:UIControlStateNormal];
        @weakify(self);
        [[_finishBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self selectInfos:self.contentDic];
            [self removeFromSuperview];
        }];
    }
    return _finishBtn;
}

- (UIPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}

- (NSMutableDictionary *)contentDic{
    if (!_contentDic) {
        _contentDic = [NSMutableDictionary dictionary];
    }
    return _contentDic;
};

- (void)setDataSource:(NSArray<NSArray<id> *> *)dataSource{
    _dataSource = dataSource;
    [self.pickerView reloadAllComponents];
    for (int i = 0; i < dataSource.count; i++) {
        [self.contentDic setObject:dataSource[i].firstObject forKey:@(i)];
    }
}

@end
